<?php

namespace App\Http\Controllers;
use App\Hospital\Metrics\Examples;

use App\Jobs\AppointList;
use App\Jobs\AppointNumJob;
use App\Models\FamilyEdu;
use App\Models\Hospital;
use App\Models\HospitalAppoint;
use App\Models\HospitalImportBigchild;
use App\Models\HospitalImportChild;
use App\Models\HospitalImportPregn;
use App\Models\HospitalUserFamily;
use App\Models\ImportFamily;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Card;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HospitalController extends Controller
{
    public function IndexData(Request $request)
    {
        $hospital_id = $request->input('hospital_id');
        $signDay = HospitalUserFamily::query()->where('hospital_id',$hospital_id)
            ->select(['starttime',DB::raw('count(*) as count')])
            ->where('status',1)
            ->groupBy('starttime')
            //->orderBy('starttime','asc')
            ->get()
            ->toArray();
        $sign1 = HospitalUserFamily::query()->where('eb_hospital_user_family.hospital_id',$hospital_id)
            ->join('eb_family', "eb_hospital_user_family.family_id", '=', "eb_family.id")
            ->where('eb_hospital_user_family.status',1)
            ->where('eb_hospital_user_family.type',1)
            ->where('eb_family.birthday','>=',date('Y-m-d',strtotime("-3 year")))
            ->count();
        $sign2 = HospitalUserFamily::query()->where('eb_hospital_user_family.hospital_id',$hospital_id)
            ->join('eb_family', "eb_hospital_user_family.family_id", '=', "eb_family.id")
            ->where('eb_hospital_user_family.status',1)
            ->where('eb_hospital_user_family.type',1)
            ->where('eb_family.birthday','<=',date('Y-m-d',strtotime("-3 year")))
            ->where('eb_family.birthday','>=',date('Y-m-d',strtotime("-7 year")))
            ->count();
        $sign = HospitalUserFamily::query()->where('hospital_id',$hospital_id)
            ->where('status',1)
            ->count();
        return new JsonResponse(['code'=>200,'msg'=>'成功','data'=>[
            'card1'=>['list'=>$signDay,'totle'=>$sign],
            'card2'=>[$sign1,$sign2],
            'child'=>Examples\IndexData::child($hospital_id),
            'bigChild'=>Examples\IndexData::bigChild($hospital_id),
            'preg'=>Examples\IndexData::preg($hospital_id),
            'guide'=>Examples\IndexData::guide($hospital_id),
        ]],200);
    }
    public function SetupScheduling()
    {
        $hospitalAppoints = HospitalAppoint::query()->where('is_open',1)
            ->get();
        if($hospitalAppoints->isNotEmpty()){
            foreach($hospitalAppoints as $hospitalAppoint){
                AppointNumJob::dispatch($hospitalAppoint->id,true);
            }
        }
        return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
    }

    public function doctors(Request $request)
    {
        $search = $request->input('search');
        $hospitals = Hospital::query()
            ->where('name','like',"%{$search}%")
            ->get();
        return view('hospitalSearch',['hospitals'=>$hospitals]);
    }

}
